package com.senlan.water.controller.appeal;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.senlan.water.common.result.BaseResult;
import com.senlan.water.dao.domain.CustomerAppeal;
import com.senlan.water.dao.service.CustomerAppealService;
import com.senlan.water.dto.appeal.CustomerAppealAddDTO;
import com.senlan.water.dto.appeal.CustomerAppealEditDTO;
import com.senlan.water.dto.appeal.CustomerAppealExcelDTO;
import com.senlan.water.dto.appeal.CustomerAppealSearchDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/appeal")
@Slf4j
@SaCheckLogin
public class CustomerAppealController {

    @Resource
    private CustomerAppealService customerAppealService;

    /**
     * 新增客户诉求
     * @param appealAddDTO
     * @return
     */
    @PostMapping("add")
    public BaseResult<Boolean> add(@RequestBody CustomerAppealAddDTO appealAddDTO){
        return BaseResult.data(customerAppealService.addNew(appealAddDTO));
    }

    /**
     * 编辑
     * @param editDTO
     * @return
     */
    @PostMapping("edit")
    public BaseResult<Boolean> edit(@RequestBody CustomerAppealEditDTO editDTO){
        return BaseResult.data(customerAppealService.editById(editDTO));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("delete")
    public BaseResult<Boolean> delete(@RequestParam("id") Long id){
        return BaseResult.data(customerAppealService.deleteById(id));
    }

    /**
     * 查询全部
     * @return
     */
    @PostMapping("getAll")
    public BaseResult<List<CustomerAppeal>> getAll() {
        return BaseResult.data(customerAppealService.list());
    }

    /**
     * 分页查询
     * @return
     */
    @PostMapping("getPage")
    public BaseResult<Page<CustomerAppeal>> getPage(@RequestBody CustomerAppealSearchDTO searchDTO) {
        return BaseResult.data(customerAppealService.getPage(searchDTO));
    }

    /**
     * 导出
     * @param response
     * @param ids
     */
    @PostMapping("export")
    public void exportExcel(HttpServletResponse response, @RequestBody List<Long> ids) {
        List<CustomerAppeal> list = CollectionUtil.isEmpty(ids)? customerAppealService.list() : customerAppealService.listByIds(ids);

        //组装excel数据
        List<CustomerAppealExcelDTO> excelDataList = new ArrayList<>();
        for (CustomerAppeal customerAppeal : list){
            CustomerAppealExcelDTO excelDTO = new CustomerAppealExcelDTO();
            BeanUtil.copyProperties(customerAppeal,excelDTO);
            excelDataList.add(excelDTO);
        }
        //导出excel
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("客户诉求", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), CustomerAppealExcelDTO.class).sheet("模板").doWrite(excelDataList);
        } catch (Exception e) {
            log.error("导出excel失败{}",e.getMessage(),e);
        }
    }


}
